1613C - Poisoned Dagger - CodeForces Solution


binary search *1200

Please click on ads to support us..

Python Code:

def dealt(k, b):
    lost = 0
    for x in b:
        if k <= x:
            continue
        else:
            lost += k - x
    if k * n - lost >= h:
        return True
    else:
        return False

T = int(input())
for _ in range(T):
    n, h = map(int, input().split())
    a = list(map(int, input().split()))
    b = []
    for i in range(n-1):
        b.append(a[i+1] - a[i])
    low = 0
    if h % n == 0:
        low = h // n
    else:
        low = h // n + 1
    high = h
    while low <= high:
        mid = (low + high) // 2
        ans = dealt(mid, b)
        ans_1 = dealt(mid-1, b)
        if ans and not ans_1:
            print(mid)
            break
        elif ans:
            high = mid - 1
        else:
            low = mid + 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
bool is_possible(vector<ll>& v,ll n,ll h){
   ll ct = 0;
   for(int i=1;i<v.size();i++){
      ct += min(n,(v[i]-v[i-1]));
   }
   ct += n;
   if(ct>=h) return true;
   else return false;
}
void solve(){
   ll n,h;
   cin>>n>>h;
   vector<ll> v(n);
   for(ll i=0;i<n;i++) cin>>v[i];
   ll lo = 1,hi = h,ans=h;
   while(lo<=hi){
      ll mid = (hi+lo)/2;
      if(is_possible(v,mid,h)){
         ans = min(mid,ans);
         hi = mid-1;
      }
      else lo = mid+1;
   }
   cout<<ans<<endl;
}
int main()
{
   IOS
   //precomp();
   ll t;
   cin>>t;
   while(t--) solve();
   return 0;
}


Comments

Submit
0 Comments
More Questions

1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring